5장 협업을 위한 git 명령어


브랜치 생성하기

git log --pretty=oneline --graph
>>
* 39acfb57afad389090746cdf1b64ef05e879fc1e (HEAD -> main, origin/main) Add hotline to main page
* 3bbf5ebc2decd19d8877fa93ae1ff0b1b7b94350 Change the title of main page
* 0ab050611561bc6b1d8b63a444f9aa5a7bae2871 Add initial files and .gitignore

Attachments/Picture/Pasted image 20240302214550.png

브랜치 생성방법 2가지

  1. 깃허브 원격 저장소에서 생성 후, 지역 저장소로 가져오기
  2. 지역저장소에서 생성 후, 원격 저장소에 반영하기

깃허브 원격 저장소에서 브랜치 생성하기

먼저 깃허브에서 브랜치 생성해줌.
git remote update 명령어로 지역 저장소에 원격 저장소의 상태를 갱신

git remote update
>>
Fetching origin
From github.com:usablechan/mastering-git-github
 * [new branch]      test/remote-branch -> origin/test/remote-branch

원격 저장소의 최신정보를 가져옴
위의 결과를 보면 새로운 브랜치 만들었다는 정보를 가져왔음.
git branch -a : 지역 저장소와 원격 저장소의 브랜치 정보를 확인함. -a 옵션은 모두 보여줌.

git branch -a
>>
* main 1️⃣
  remotes/origin/main2️⃣
  remotes/origin/test/remote-branch3️⃣

1️⃣ 은 지역저장소의 main 브랜치를 의미함. * 표시가 현재 작업 중인 브랜치 라는 뜻
2️⃣ , 3️⃣ 처럼 remote/origin 붙은 브랜치가 원격 저장소의 브랜치

git checkout -t <브랜치명> 으로 원격 저장소 브랜치를 지역 저장소의 브랜치로 설정

git checkout -t origin/test/remote-branch
>>
Branch 'test/remote-branch' set up to track remote branch 'test/remote-branch' from 'origin'.
Switched to a new branch 'test/remote-branch'

실행후 지역 저장소의 브랜치 변화

git branch -a
>>
main
* test/remote-branch
  remotes/origin/main
  remotes/origin/test/remote-branch

➕ git checkout 명령어 옵션
-b : 브랜치 생성하고 사용할 브랜치로 지정
-t : 원격저장소 브랜치를 지역 저장소에서 사용할 브랜치로 지정

➕ git branch 명령어 옵션
-a : 지역 저장소, 원격 저장소 브랜치 정보 같이 보기
-d : 브랜치 삭제
-l : 지역 저장소 브랜치 정보만 보여줌
-r : 원격 저장소 브랜치 정보만 보여줌
-v : 지역 저장소의 브랜치 정보를 커밋 내역과 같이 보여줌
-m : 브랜치 이름 변경

지역 저장소에서 브랜치 생성

git branch {새로운 브랜치명} 으로 브랜치 생성

git branch -a
>>
* main
  test/local-branch
  test/remote-branch
  remotes/origin/main
  remotes/origin/test/remote-branch

remote/origin/ 접두사 붙은 local-branch없으니까 원격 저장소에 반영 안된거임.

git checkout 명령어 이용해서 작업브랜치로 변경하고
git push로 새로운 브랜치를 github에 등록

git checkout test/local-branch
>> Switched to branch 'test/local-branch'
git push origin test/local-branch
>>
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'test/local-branch' on GitHub by visiting:
remote:      https://github.com/usablechan/mastering-git-github/pull/new/test/local-branch
remote:
To github.com:usablechan/mastering-git-github.git
 * [new branch]      test/local-branch -> test/local-branch

브랜치 삭제하기

git branch -d {브랜치명} : 지역 저장소의 브랜치 삭제
git push origin -d {브랜치명} : 원격 저장소의 브랜치 삭제
➕ 원격 저장소 브랜치 삭제하려면 remotes/origin/ 떼고 브랜치명 넘겨줘야함.

git branch test/deleted
git branch -d test/deleted
>> Deleted branch test/deleted (was 39acfb5).

브랜치 병합하기

새로운 작업 브랜치의 커밋내역을 기준 브랜치에 반영하는 작업을 브랜치 병합 이라고 한다.
Attachments/Picture/Pasted image 20240302233814.png
지역 저장소 test/local-branch 도 main 브랜치와 동일한 커밋을 바라봄.
→ test/local-branch를 작업 브랜치로 변경하여 새로운 커밋 생성한 후에 main브랜치에 반영 필요함.
Attachments/Picture/Pasted image 20240302233835.png
1번 커밋은 test/local-branch 에서 생성한 커밋이다. 이 커밋은 main 브랜치가 알 수 없으므로 작업 완료 후에 main 브랜치에 반영하는 작업이 필요함.
2번 머지 커밋이라는 새로운 커밋을 만들어서 병합함.

빨리감기 병합 : fast forward

main 브랜치 기준으로 작업 브랜치 생성하고, 작업 완료하여 main 브랜치에 병합시도.
이때 main 브랜치에 새로운 커밋 없으면 빨리감기 병합으로 진행.

git branch test/fast-forward
git checkout test/fast-forward

test/fast-forward 브랜치 만들고 수정한다음에 addcommit 해줬음

git log --pretty=oneline --graph
>>
* af71cbb9a9e2103e3636feaf3341f33c1d390cf3 (HEAD -> test/fast-forward) Change title 1️⃣
* 39acfb57afad389090746cdf1b64ef05e879fc1e (origin/test/remote-branch, origin/test/local-branch, origin/main, test/remote-branch, test/local-branch, main) Add hotline to main page 2️⃣
* 3bbf5ebc2decd19d8877fa93ae1ff0b1b7b94350 Change the title of main page
* 0ab050611561bc6b1d8b63a444f9aa5a7bae2871 Add initial files and .gitignore

1️⃣ test/fast-forward만 최근 생성한 커밋 바라보고있음.
2️⃣ main 이랑 그 외 브랜치들은 이전 커밋 바라봄.

git checkout main
git log --pretty=oneline --graph
>> 
* 39acfb57afad389090746cdf1b64ef05e879fc1e (HEAD -> main, origin/test/remote-branch, origin/test/local-branch, origin/main, test/remote-branch, test/local-branch) Add hotline to main page
* 3bbf5ebc2decd19d8877fa93ae1ff0b1b7b94350 Change the title of main page
* 0ab050611561bc6b1d8b63a444f9aa5a7bae2871 Add initial files and .gitignore

main 브랜치로 바꾸고 내역 보면 새로운 브랜치의 작업 내용 보이지 않음
→ 아직 병합하지 않아서 그럼.

git merge 이용해서 병합

git merge test/fast-forward
>>
Updating 39acfb5..af71cbb
Fast-forward
 public/index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git log --pretty=oneline --graph
>>
* af71cbb9a9e2103e3636feaf3341f33c1d390cf3 (HEAD -> main, test/fast-forward) Change title
* 39acfb57afad389090746cdf1b64ef05e879fc1e (origin/test/remote-branch, origin/test/local-branch, origin/main, test/remote-branch, test/local-branch) Add hotline to main page
* 3bbf5ebc2decd19d8877fa93ae1ff0b1b7b94350 Change the title of main page
* 0ab050611561bc6b1d8b63a444f9aa5a7bae2871 Add initial files and .gitignore

main 이 머지 커밋을 바로보고 있는 모습.
Attachments/Picture/Pasted image 20240302233934.png

병합 커밋 생성 : merge commit

기준 브랜치에 변경이 존재하는 경우에 사용.
기준 브랜치와 새로운 작업 브랜치의 변경 내용을 하나로 합쳐야 함.

git checkout test/local-branch
git log --pretty=oneline --graph --all
>>
* af71cbb9a9e2103e3636feaf3341f33c1d390cf3 (HEAD -> main, test/fast-forward) Change title
* 39acfb57afad389090746cdf1b64ef05e879fc1e (origin/test/remote-branch, origin/test/local-branch, origin/main, test/remote-branch, test/local-branch) Add hotline to main page
* 3bbf5ebc2decd19d8877fa93ae1ff0b1b7b94350 Change the title of main page
* 0ab050611561bc6b1d8b63a444f9aa5a7bae2871 Add initial files and .gitignore

test/local-branch와 main이 다른 커밋을 바라보고 있고, main 브랜치에 변경내용이 존재함.

code 수정하고 addcommit 해줌.

git checkout main
git merge test/local-branch
>>
Auto-merging public/index.html
Merge made by the 'recursive' strategy.
 public/index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git log --pretty=oneline --graph
>>
*   f53290cd33ea94479bd6853425fab1e42530b1ce (HEAD -> main) Merge branch 'test/local-branch' into main 1️⃣
|\
| * 36d7a418753409f666d3e12ad6a13d9268555a99 (test/local-branch) Change haeder 2️⃣
* | af71cbb9a9e2103e3636feaf3341f33c1d390cf3 (test/fast-forward) Change title 3️⃣
|/ 
* 39acfb57afad389090746cdf1b64ef05e879fc1e (origin/test/remote-branch, origin/test/local-branch, origin/main, test/remote-branch) Add hotline to main page
* 3bbf5ebc2decd19d8877fa93ae1ff0b1b7b94350 Change the title of main page
* 0ab050611561bc6b1d8b63a444f9aa5a7bae2871 Add initial files and .gitignore

fast-forward와 다르게 아예 새로운 머지 커밋이 생성되어서 main 브랜치가 가르키게됨.
Attachments/Picture/Pasted image 20240302234000.png

충돌 해결하기

충돌할 수도 있음.

git checkout test/remote-branch
git log --pretty=oneline --graph --all
>>
*   f53290cd33ea94479bd6853425fab1e42530b1ce (main) Merge branch 'test/local-branch' into main
|\
| * 36d7a418753409f666d3e12ad6a13d9268555a99 (test/local-branch) Change haeder
* | af71cbb9a9e2103e3636feaf3341f33c1d390cf3 (test/fast-forward) Change title
|/
* 39acfb57afad389090746cdf1b64ef05e879fc1e (HEAD -> test/remote-branch, origin/test/remote-branch, origin/test/local-branch, origin/main) Add hotline to main page
* 3bbf5ebc2decd19d8877fa93ae1ff0b1b7b94350 Change the title of main page
* 0ab050611561bc6b1d8b63a444f9aa5a7bae2871 Add initial files and .gitignore

또 수정하고 addcommit

git checkout main
git merge test/remote-branch
>>
Auto-merging public/index.html
CONFLICT (content): Merge conflict in public/index.html
Automatic merge failed; fix conflicts and then commit the result.

오류 발생함.
수정파일 가보면 충돌 내용 알려줌 개신기 ㅋ
알아서 충돌내용 지워주고 다시 main 에서 commit 하면 됨.

풀 리퀘스트 요청하기

함께 작업하는 동료들에게 브랜치 병합 예정인 변경내역 검토를 요청할 수 있음.

git hub에서 pull request 탭에서 원래 브랜치와 변경 브랜치 골라서 pull request 생성가능
리뷰 받고 승인 받을 수 있더라~

merge pull request 해서 병합하면 됨

원격 저장소 최신내역을 지역 저장소 브랜치 반영하기

git pull {원격 저장소 식별자} {원격 저장소 브랜치}

git checkout main
git pull origin main
>>
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), 635 bytes | 635.00 KiB/s, done.
From github.com:usablechan/mastering-git-github
 * branch            main       -> FETCH_HEAD
   39acfb5..00e319d  main       -> origin/main
Updating 3cf27f2..00e319d
Fast-forward
 public/index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

git pull vs fetch

git pull 은 원격저장소의 변경 내역 가져와서 지역 저장소의 작업 브랜치에 병합까지 완료
git fetch 는 원격 저장소의 변경 내역을 가져오기만 하고 지역 저장소의 작업 브랜치에 병합은 하지 않음
→ 병합은 직접 git merge 로 해야함.